package com.rtmart.imageupload.repositoryImp;

import com.rtmart.imageupload.entity.Supplier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class SupplierRepositoryImp {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<Supplier> supplierList(){
        String sql = "select * from supplier";
        return jdbcTemplate.query(sql,new Object[]{},new BeanPropertyRowMapper<>(Supplier.class));
    }

    public void update(Long[] id, String[] name, Integer[] state){
        String sql = "update supplier set name=?,state=? where id=?";
        for(int i=0;i<id.length; i++){
            jdbcTemplate.update(sql, name[i], state[i], id[i]);
        }
    }
    public Integer ajaxUpdate(Long id,String name,Integer state){
        String sql = "update supplier set";
        List <Object> queryList=new ArrayList<Object>();
        if (name != null){
            sql += " name=? ";
            queryList.add(name);
        }
        if (state != null){
            sql += " state=? ";
            queryList.add(state);
        }
        sql += " where id=?";
        queryList.add(id);
        return jdbcTemplate.update(sql,queryList.toArray());
    }
}
